//
//  AdController.m
//  HybidMediation
//
//  Created by jay Win on 2019/10/15.
//  Copyright © 2019 jay Win. All rights reserved.
//

#import "ADController.h"



@implementation ADController

+ (instancetype)instance{
    static ADController *manager=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [ADController new];
        [KKADManager shareInstance].delegate=manager;
    });
    return manager;
}

#pragma mark   广告控制器回调
- (void)onAdsListener:(nonnull NSString *)adsState {
     [self sendMessage:adsState];
}
- (void)initMopub:(NSNumber *)debug{
  [[KKADManager shareInstance]initAds:YES debugModel:debug.intValue==0?NO:YES];
}
-(void)getMessage{
    NSString *trackName=[[NSUserDefaults standardUserDefaults]stringForKey:@"KK_AD_SOURCE"];
    if(trackName){
        NSLog(@"============trackName:======%@",trackName);
    }else{
        NSLog(@"");
    }
  [self sendMessageToLaya:@"SendTrackerInfo" datastr:trackName];
}
- (void)sendMessageToLaya:(NSString *)type datastr:(NSString *)data{
    #ifdef RUN_WITH_LAYA
    NSDictionary *dic = @{@"key":type,@"data":data};
    NSString * json = [self dictToJSONString:dic];
    [[conchRuntime GetIOSConchRuntime]callbackToJSWithObject:self methodName:@"getMessage" ret:json];
    #endif
}
-(NSString *)dictToJSONString:(NSObject *)dic {
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //    NSString *resultString =[jsonString stringByReplacingOccurrencesOfString:@"" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return jsonString;
}

- (void)sendMessage:(NSString *)message{
#ifdef RUN_WITH_UNITY
    UnitySendMessage("AdsManager", "Callback", [message UTF8String]);
#endif
    
#ifdef RUN_WITH_LAYA
  [[conchRuntime GetIOSConchRuntime]callbackToJSWithObject:self methodName:@"initMopub:" ret:message];
#endif
}


- (void)showInterstitial{
   #ifdef RUN_WITH_LAYA
     [[KKADManager shareInstance]showInterstitial:[ViewController GetIOSViewController]];
    #endif
}
-(void)showRewardVideo{
#ifdef RUN_WITH_LAYA
    [[KKADManager shareInstance]showRewardVideo: [ViewController GetIOSViewController]];
#endif
}


-(void)requestRewardVideo{
   NSLog(@"=======函数已废弃");
}
-(void)setAdsInterval:(NSNumber *)time{
    NSLog(@"=======函数已废弃");
}

- (void)getIDFA{
#ifdef RUN_WITH_LAYA
     NSString *idfa=[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    [[conchRuntime GetIOSConchRuntime]callbackToJSWithObject:self methodName:@"getIDFA" ret:idfa];
#endif
}
-(void)agreeGDPR:(NSNumber*)status{
    [[KKADManager shareInstance]agreeGDPR:status.intValue==0?NO:YES];
}
-(void)agreeCOPPA:(NSNumber*)status{
    [[KKADManager shareInstance]agreeCOPPA:status.intValue==0?NO:YES];
}

extern "C"{
    
    void initAds(bool useLocalConfig,bool debug){
        [[KKADManager shareInstance]initAds:useLocalConfig debugModel:debug];
    }
    void showBanner(bool top){
#ifdef RUN_WITH_UNITY
         [[KKADManager shareInstance]showBanner:UnityGetGLViewController() bannerContainer:UnityGetGLView() isTop:top];
#endif
    }
    void loadSplash(){
#ifdef RUN_WITH_UNITY
     [[KKADManager shareInstance]loadSplash:UnityGetGLViewController()];
#endif
    }

    void hideBanner(){
        [[KKADManager shareInstance]hideBanner];
    }
    bool hasInterstitial(){
        return [[KKADManager shareInstance]hasInterstitial];
    }
    void showInterstitial(){
#ifdef RUN_WITH_UNITY
        [[KKADManager shareInstance]showInterstitial:UnityGetGLViewController()];
#endif
    }
    bool hasRewardVideo(){
        return [[KKADManager shareInstance]hasRewardVideo];;
    }
    void showRewardVideo(){
#ifdef RUN_WITH_UNITY
        [[KKADManager shareInstance]showRewardVideo:UnityGetGLViewController()];
#endif
    }
    void setRewardVideoAutoPlayWhenLoadedStatus(bool enable){

    }
    void agreeGDPR(bool state){
        [[KKADManager shareInstance]agreeGDPR:state];
    }
    void agreeCOPPA(bool state){
         [[KKADManager shareInstance]agreeCOPPA:state];
    }
    const char *getIDFA(){
        NSString *idfa=[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        if(idfa){
            return  [idfa UTF8String];
        }else{
            return  "";
        }
    }
}

@end
